package com.example.sociamedia.interaction.controller;

import com.example.sociamedia.interaction.dto.PrivateMessageRequest;
import com.example.sociamedia.interaction.dto.PrivateMessageResponse;
import com.example.sociamedia.interaction.entity.PrivateMessage;
import com.example.sociamedia.interaction.mapper.PrivateMessageMapper;
import com.example.sociamedia.user.entity.User;
import com.example.sociamedia.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.util.List;

@Service
public class PrivateMessageService implements PrivateMessageApi {
    private final PrivateMessageMapper privateMessageMapper;
    private final UserMapper userMapper;

    @Autowired
    public PrivateMessageService(PrivateMessageMapper privateMessageMapper, UserMapper userMapper) {
        this.privateMessageMapper = privateMessageMapper;
        this.userMapper = userMapper;
    }

    @Override
    public PrivateMessageResponse sendMessage(String sessionToken, PrivateMessageRequest request) {
        User sender = userMapper.selectUserByToken(sessionToken);
        PrivateMessageResponse resp = new PrivateMessageResponse();
        if (sender == null) {
            resp.setSuccess(false);
            resp.setMessage("无效的用户token");
            return resp;
        }
        if (request.getReceiverId() == null || (request.getContent() == null && request.getPostId() == null)) {
            resp.setSuccess(false);
            resp.setMessage("收信人和内容/分享帖子至少有一个不能为空");
            return resp;
        }
        PrivateMessage message = new PrivateMessage();
        message.setSenderId(sender.getUserid());
        message.setReceiverId(request.getReceiverId());
        message.setContent(request.getContent());
        message.setPostId(request.getPostId());
        privateMessageMapper.insertMessage(message);
        resp.setSuccess(true);
        resp.setMessage("发送成功");
        resp.setMessageId(message.getMessageId());
        resp.setSenderId(message.getSenderId());
        resp.setReceiverId(message.getReceiverId());
        resp.setContent(message.getContent());
        resp.setCreatedAt(new Timestamp(System.currentTimeMillis()));
        resp.setPostId(message.getPostId());
        return resp;
    }

    @Override
    public List<PrivateMessage> getMessagesWithUser(String sessionToken, Long otherUserId) {
        User user = userMapper.selectUserByToken(sessionToken);
        if (user == null) {
            return List.of();
        }
        return privateMessageMapper.selectMessagesBetween(user.getUserid(), otherUserId);
    }

    @Override
    public List<PrivateMessage> getConversations(String sessionToken) {
        User user = userMapper.selectUserByToken(sessionToken);
        if (user == null) {
            return List.of();
        }
        return privateMessageMapper.selectLatestConversations(user.getUserid());
    }

    public int countUnreadMessages(String sessionToken) {
        User user = userMapper.selectUserByToken(sessionToken);
        if (user == null) return 0;
        return privateMessageMapper.countUnreadMessages(user.getUserid());
    }

    public int markMessagesAsRead(String sessionToken, Long senderId) {
        User user = userMapper.selectUserByToken(sessionToken);
        if (user == null) return 0;
        return privateMessageMapper.markMessagesAsRead(user.getUserid(), senderId);
    }

    public List<java.util.Map<String, Object>> getUnreadList(String sessionToken) {
        User user = userMapper.selectUserByToken(sessionToken);
        if (user == null) return List.of();
        return privateMessageMapper.selectUnreadCountBySender(user.getUserid());
    }
} 